package com.landeb.shop.huo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.landeb.shop.R;
import com.landeb.shop.huo.bean.BeanFragShoppingCart;

import java.util.List;
import java.util.Map;

/**
 * Created by blade on 2016/10/20.
 */

public class FragShoppingCartAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    //店铺列表
    private List<BeanFragShoppingCart.DataBean.ListBean> mGroups;
    //map key是店铺id value是该所购买店铺的商品的list
    private Map<String, List<BeanFragShoppingCart.DataBean.ListBean.ChildBean>> mChildren;

    //    向外暴露的接口 非别用于检测项目选中和商品的数量变化
    private CheckInterface mCheckInterface;
    private ModifyInterface mModifyInterface;

    public FragShoppingCartAdapter(Context context, List<BeanFragShoppingCart.DataBean.ListBean> groups, Map<String, List<BeanFragShoppingCart.DataBean.ListBean.ChildBean>> children) {
        mContext = context;
        mGroups = groups;
        mChildren = children;
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        mCheckInterface = checkInterface;
    }

    public void setModifyInterface(ModifyInterface modifyInterface) {
        mModifyInterface = modifyInterface;
    }

    @Override
    public int getGroupCount() {
        return mGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mChildren.get(mGroups.get(groupPosition).getId()).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return mChildren.get(mGroups.get(groupPosition).getId()).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        final GroupHolder groupHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_group_frag_shopping_cart_expand_list, null);

            groupHolder = new GroupHolder();

            groupHolder.shopCheckBox = (CheckBox) convertView.findViewById(R.id.cb_item_group_frag_shopping_cart_expand_list);
            groupHolder.shopName = (TextView) convertView.findViewById(R.id.tv_shopName_item_group_frag_shopping_cart_expand_list);

            convertView.setTag(groupHolder);

        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        final BeanFragShoppingCart.DataBean.ListBean shop = (BeanFragShoppingCart.DataBean.ListBean) getGroup(groupPosition);

        groupHolder.shopName.setText(shop.getStorename());

        /**修改起始状态选中*/

        boolean shopChecked;
        if (shop.getIs_check() == 1) {
            shopChecked = true;
        } else {
            shopChecked = false;
        }
        groupHolder.shopCheckBox.setChecked(shopChecked);

        groupHolder.shopCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                shop.setChoosed(((CheckBox) v).isChecked());

                if (((CheckBox) v).isChecked()) {
                    shop.setIs_check(1);
                } else {
                    shop.setIs_check(2);
                }

//                checkBox的isChecked的方法获取到checkbox的选中状态，将状态提供给外层frag
                mCheckInterface.checkedGroup(groupPosition, ((CheckBox) v).isChecked());
            }
        });

        notifyDataSetChanged();
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        final ChildHolder childHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_child_frag_shopping_cart_expand_list, null);
            childHolder = new ChildHolder();

            childHolder.goodsCheckBox = (CheckBox) convertView.findViewById(R.id.cb_goods_item_child_frag_shopping_cart_expand_list);
            childHolder.goodsPic = (ImageView) convertView.findViewById(R.id.iv_goods_item_child_frag_shopping_cart_expand_list);
            childHolder.goodsName = (TextView) convertView.findViewById(R.id.tv_goods_title_item_child_frag_shopping_cart_expand_list);
            childHolder.goodsPrice = (TextView) convertView.findViewById(R.id.tv_goods_price_item_child_frag_shopping_cart_expand_list);
            childHolder.goodsNUm = (TextView) convertView.findViewById(R.id.tv_goods_num_item_child_frag_shopping_cart_expand_list);
            childHolder.addGoodsBtn = (Button) convertView.findViewById(R.id.btn_add_item_child_frag_shopping_cart_expand_list);
            childHolder.deleteGoodsBtn = (Button) convertView.findViewById(R.id.btn_reduce_item_child_frag_shopping_cart_expand_list);

            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        final BeanFragShoppingCart.DataBean.ListBean.ChildBean goods = (BeanFragShoppingCart.DataBean.ListBean.ChildBean) getChild(groupPosition, childPosition);

        /**修改起始状态的选中*/
        boolean goodsChecked;
        if (goods.getIs_check() == 1) {
            goodsChecked = true;
        } else {
            goodsChecked = false;
        }
        childHolder.goodsCheckBox.setChecked(goodsChecked);


        // TODO: 2016/10/20 图片加载
//        childHolder.goodsPic.setImageResource(R.mipmap.ic_launcher);

        Glide.with(mContext)
                .load(goods.getPic())
                .placeholder(R.drawable.loading_pic)
                .error(R.drawable.error_pic)
                .into(childHolder.goodsPic);

        childHolder.goodsName.setText(goods.getName());
        childHolder.goodsPrice.setText("￥" + goods.getPrice());
        childHolder.goodsNUm.setText(goods.getAmount() + "");

        childHolder.goodsCheckBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int goodsChecked;
                if (((CheckBox) v).isChecked()) {
                    goodsChecked = 1;
                } else {
                    goodsChecked = 2;
                }
                goods.setIs_check(goodsChecked);
//                goods.setChoosed(((CheckBox) v).isChecked());
                childHolder.goodsCheckBox.setChecked(((CheckBox) v).isChecked());
                // 将子checkBox的选中状态暴露出去
                mCheckInterface.checkedChild(groupPosition, childPosition, ((CheckBox) v).isChecked());
            }
        });

        childHolder.goodsNUm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        childHolder.addGoodsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 暴露增加接口
                mModifyInterface.doIncrease(groupPosition, childPosition, childHolder.goodsNUm, childHolder.goodsCheckBox.isChecked());
            }
        });

        childHolder.deleteGoodsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 暴露删减接口
                mModifyInterface.doDecrease(groupPosition, childPosition, childHolder.goodsNUm, childHolder.goodsCheckBox.isChecked());

            }
        });


        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    /**
     * 复选框接口
     */
    public interface CheckInterface {
        /**
         * 组选框状态改变触发的事件
         *
         * @param groupPosition 组元素位置
         * @param isChecked     组元素选中与否
         */
        void checkedGroup(int groupPosition, boolean isChecked);

        /**
         * 子选框状态改变时触发的事件
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param isChecked     子元素选中与否
         */
        void checkedChild(int groupPosition, int childPosition, boolean isChecked);
    }

    /**
     * 改变数量的接口
     */
    public interface ModifyInterface {
        /**
         * 增加操作
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);

        /**
         * 删减操作
         *
         * @param groupPosition 组元素位置
         * @param childPosition 子元素位置
         * @param showCountView 用于展示变化后数量的View
         * @param isChecked     子元素选中与否
         */
        void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);

    }


    class GroupHolder {
        private CheckBox shopCheckBox;
        private TextView shopName;
    }


    class ChildHolder {
        private CheckBox goodsCheckBox;
        private ImageView goodsPic;
        private TextView goodsName;
        private TextView goodsPrice;
        private TextView goodsNUm;
        private Button addGoodsBtn;
        private Button deleteGoodsBtn;

    }
}
